var express = require('express');
var router = express.Router();
const studentServices = require("../src/services/student/studentServices")

/* GET users listing. */
router.get('/shows', async function(req, resp) {
 let res=  await  studentServices.showAllStudentsInfo();
 
  resp.json({
    "status": 0,
    "message": "", 
    "total": 180, 
    "datas": res 
   })
});
//添加
router.post("/addstudent",async(req,resp)=>{
   let {name,age,gender,address,score} =req.body;
   let res=  await  studentServices.addStudent({name,age,gender,address,score});
   if(res!=null){
     resp.json({"code":1,"msg":"添加成功"})
   }else{
    resp.json({"code":0,"msg":"添加失败"})
   }
})

//删除学生
router.delete("/delstudent/:id",async (req,resp)=>{
    let {id} = req.params;
    let res=  await  studentServices.delStudentInfo(id)
   if(res.deletedCount==1){
     resp.json({"code":1,"msg":"删除成功"})
   }else{
    resp.json({"code":0,"msg":"删除失败"})
   } 
})


//查看学生详情
router.get("/showstudentDetailInfo/:studentid", async (req,resp)=>{
    let {studentid}= req.params;
    let res=  await  studentServices.showStudentDetailInfo(studentid)
    if(res!=null){
      resp.json({"code":1,"msg":"查询成功","data":res})
    }else{
     resp.json({"code":0,"msg":"查询失败"})
    } 
})
//修改学生信息
router.put("/updatestudentInfo", async (req,resp)=>{
  //获得参数
  let {_id,name,age,address,score,gender}= req.body;
  let res=  await  studentServices.updateStudentInfo({_id,name,age,address,score,gender});
  if(res.modifiedCount==1){
    resp.json({"code":1,"msg":"修改成功","data":res})
  }else{
   resp.json({"code":0,"msg":"修改失败"})
  } 
})



module.exports = router;
